package com.mstore.service;

import java.util.ArrayList;
import java.util.List;

import com.mstore.dao.ConsumeDao;
import com.mstore.pojo.Consume;

public class ConsumeService {

	private ConsumeDao consumeDao;

	public void addConsume(Consume consume) {
		consumeDao.add(consume);
	}

	/**
	 * 查询某店铺的消费记录
	 */
	public List<Consume> getConsumePage(int index, Consume consume) {
		List<Object> objects = consumeDao.getConsumePage(index, consume);
		List<Consume> consumes = new ArrayList<Consume>();
		for (Object object : objects) {
			Consume co = (Consume) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			consumes.add(co);
		}
		return consumes;
	}

	/**
	 * 查询企业的消费记录
	 */
	public List<Consume> getEnterpriseConsumePage(int index, Consume consume) {
		List<Object> objects = consumeDao.getEnterpriseConsumePage(index,
				consume);
		List<Consume> consumes = new ArrayList<Consume>();
		for (Object object : objects) {
			Consume co = (Consume) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			consumes.add(co);
		}
		return consumes;
	}

	public List<Consume> getConsumesByshop(Consume consume) {
		List<Object> objects = consumeDao.getConsumesByshop(consume);
		List<Consume> consumes = new ArrayList<Consume>();
		for (Object object : objects) {
			Consume co = (Consume) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			consumes.add(co);
		}
		return consumes;
	}

	/**
	 * 查询会员的消费记录
	 */
	public List<Consume> getConsumesByMobile(int index, Consume consume) {
		List<Object> objects = consumeDao.getConsumesByMobile(index, consume);
		List<Consume> consumes = new ArrayList<Consume>();
		for (Object object : objects) {
			Consume co = (Consume) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			consumes.add(co);
		}
		return consumes;
	}

	/**
	 * 查询会员在某店铺的消费记录
	 */
	public List<Consume> getConsumesByMobileAndShop(int index, Consume consume) {
		List<Object> objects = consumeDao.getConsumesByMobileAndShop(index,
				consume);
		List<Consume> consumes = new ArrayList<Consume>();
		for (Object object : objects) {
			Consume co = (Consume) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			consumes.add(co);
		}
		return consumes;
	}

	/**
	 * 查询某店铺的消费记录条数
	 */
	public int getConsumeLine(Consume consume) {
		return consumeDao.getConsumeLine(consume);
	}

	/**
	 * 查询企业的消费记录条数
	 */
	public int getEnterpriseConsumeLine(Consume consume) {
		return consumeDao.getEnterpriseConsumeLine(consume);
	}

	/**
	 * 查询会员在某店铺的消费记录条数
	 */
	public int getOneConsumeLineByMobileAndShop(Consume consume) {
		return consumeDao.getOneConsumeLineByMobileAndShop(consume);
	}

	/**
	 * 查询会员的消费记录条数
	 */
	public int getOneConsumeLineByMobile(Consume consume) {
		return consumeDao.getOneConsumeLineByMobile(consume);
	}

	public int getEnterpriseOneConsumeLine(Consume consume) {
		return consumeDao.getEnterpriseOneConsumeLine(consume);
	}

	public List<Consume> getConsumes(Consume consume) {
		List<Object> objects = null;
		if (consume.getShopId() == 0) {
			objects = consumeDao.getEnterpriseConsumes(consume);

		}
		else {

			objects = consumeDao.getConsumes(consume);
		}
		List<Consume> consumes = new ArrayList<Consume>();
		for (Object object : objects) {
			Consume co = (Consume) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			consumes.add(co);
		}
		return consumes;
	}

	public List<Consume> getConsumesIndex(int index, Consume consume) {
		List<Object> objects = consumeDao.getConsumesIndex(index, consume);
		List<Consume> consumes = new ArrayList<Consume>();
		for (Object object : objects) {
			Consume co = (Consume) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			consumes.add(co);
		}
		return consumes;
	}

	public int getConsumePage(Consume consume) {
		return consumeDao.getConsumePage(consume);
	}

	public List<Consume> getEnterpriseConsumes(Consume consume) {
		List<Object> objects = consumeDao.getEnterpriseConsumes(consume);
		List<Consume> consumes = new ArrayList<Consume>();
		for (Object object : objects) {
			Consume co = (Consume) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			consumes.add(co);
		}
		return consumes;
	}

	public List<Consume> getOneConsumePage(int index, Consume consume) {
		List<Object> objects = consumeDao.getOneConsumePage(index, consume);
		List<Consume> consumes = new ArrayList<Consume>();
		for (Object object : objects) {
			Consume co = (Consume) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			consumes.add(co);
		}
		return consumes;
	}

	public List<Consume> getEnterpriseOneConsumePage(int index, Consume consume) {
		List<Object> objects = consumeDao.getEnterpriseOneConsumePage(index,
				consume);
		List<Consume> consumes = new ArrayList<Consume>();
		for (Object object : objects) {
			Consume co = (Consume) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			consumes.add(co);
		}
		return consumes;
	}

	public List<Consume> getConsumesBymember(Consume consume) {
		List<Object> objects = consumeDao.getConsumesBymember(consume);
		List<Consume> consumes = new ArrayList<Consume>();
		for (Object object : objects) {
			Consume co = (Consume) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			consumes.add(co);
		}
		return consumes;
	}

	public List<Consume> getByCreateTimeAndMemberId(int index, Consume consume,
			String beginTime, String endTime) {
		List<Object> objects = consumeDao.getByCreateTimeAndMemberId(index,
				consume, beginTime, endTime);
		List<Consume> consumes = new ArrayList<Consume>();
		for (Object object : objects) {
			Consume co = (Consume) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			consumes.add(co);
		}
		return consumes;
	}

	/**
	 * 查询在某企业的消费记录中的会员
	 */
	public List<Long> findMemberIdFromConsume(Long eaId) {
		return consumeDao.findMemberIdFromConsume(eaId);
	}

	public int getByCreateTimeAndMemberIdPage(Consume consume,
			String beginTime, String endTime) {
		return consumeDao.getByCreateTimeAndMemberIdPage(consume, beginTime,
				endTime);
	}

	public Consume getOneById(Consume consume) {
		Consume c = (Consume) consumeDao.getOneById(consume);
		c.setCreateTime(c.getCreateTime().substring(0,
				c.getCreateTime().indexOf(".")));
		return c;
	}

	/**
	 * 根据时间查询
	 */
	public List<Consume> getConsumePageByTime(int index, Consume consume,
			String beginTime, String endTime) {
		List<Object> objects = consumeDao.getConsumePageByTime(index, consume,
				beginTime, endTime);
		List<Consume> consumes = new ArrayList<Consume>();
		for (Object object : objects) {
			Consume co = (Consume) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			consumes.add(co);
		}
		return consumes;
	}

	/**
	 * 根据时间查询总数
	 * 
	 * @param consume
	 */
	public int getBalancePageByTimeTotal(Consume consume, String beginTime,
			String endTime) {
		return consumeDao
				.getConsumePageByTimeTotal(consume, beginTime, endTime).size();
	}

	/**
	 * 获得一段时间的信息
	 * 
	 * @param consume
	 */
	public List<Consume> getConsuemByTime(Consume consume, String beginTime,
			String endTime) {
		List<Object> objects = consumeDao.getConsumePageByTimeTotal(consume,
				beginTime, endTime);
		List<Consume> consumes = new ArrayList<Consume>();
		for (Object object : objects) {
			Consume co = (Consume) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			consumes.add(co);
		}
		return consumes;
	}

	/**
	 * 根据时间段查询
	 */
	public List<Consume> getBalancePageConsumeByTime(Consume consume,
			String beginTime, String endTime) {
		List<Object> objects = consumeDao.getBalancePageConsumeByTime(consume,
				beginTime, endTime);
		List<Consume> consumes = new ArrayList<Consume>();
		for (Object object : objects) {
			Consume co = (Consume) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			consumes.add(co);
		}
		return consumes;
	}

	/**
	 * 通过memberid和eaid来查找
	 * 
	 * @param index
	 * @param consume
	 * @return
	 */
	public List<Consume> getConsumeByIDAndEa(Consume consume) {
		List<Object> objects = consumeDao.getConsumeByIDAndEa(consume);
		List<Consume> consumes = new ArrayList<Consume>();
		for (Object object : objects) {
			Consume co = (Consume) object;
			co.setCreateTime(co.getCreateTime().substring(0,
					co.getCreateTime().indexOf(".")));
			consumes.add(co);
		}
		return consumes;
	}

	/**
	 * 查询会员在某企业里的余额消费总数
	 */
	public Double getType1ConsumeSum(Long eaId, Long memberId) {
		Object object = consumeDao.getType1ConsumeSum(eaId, memberId);
		if (object != null) {
			return (Double) object;
		}
		return 0d;
	}

	/**
	 * 查询某时间段内在企业中消费记录的总数
	 */
	public Long getCountConsumeByTime(Long eaId, String begin, String end) {
		return consumeDao.getCountConsumeByTime(eaId, begin, end);
	}

	/**
	 * 查询某时间段内在分店中消费记录的总数
	 */
	public Long getCountConsumeByTimeAndShopId(Long shopId, String begin,
			String end) {
		return consumeDao.getCountConsumeByTimeAndShopId(shopId, begin, end);
	}

	/**
	 * 查询某时间段内在企业中消费金额总量
	 */
	public Double getSumConsumeByTime(Long eaId, String begin, String end) {
		return consumeDao.getSumConsumeByTime(eaId, begin, end);
	}

	/**
	 * 查询某时间段内在分店中消费金额总量
	 */
	public Double getSumConsumeByTimeAndShopId(Long shopId, String begin,
			String end) {
		return consumeDao.getSumConsumeByTimeAndShopId(shopId, begin, end);
	}

	public void deleteById(Consume consume) {

		consumeDao.deleteById(consume);
	}

	public ConsumeDao getConsumeDao() {
		return consumeDao;
	}

	public void setConsumeDao(ConsumeDao consumeDao) {
		this.consumeDao = consumeDao;
	}
}
